<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,
        body {
            height: 100%;
        }

        .black {
            background-color: #000;
            color: pink;
        }
    </style>
</head>

<body>
    <h1 id="h1">开关灯案例</h1>
    <button id="btn">点击开关灯</button>
    <script>
        var btn = document.getElementById("btn");
        let flag = false;

        btn.onclick = () => {
            if (flag == false) {
                document.getElementsByTagName("html")[0].classList.add("black");
                flag = true;
            } else {
                document.getElementsByTagName("html")[0].classList.remove("black");
                flag = false;
            }
        }
    </script>
</body>

</html>